define(function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var _this = this;
        return {
          formdata: {},
          urls: {
            index: backend.workflowService + '/api/nwfflow/',
            start: backend.workflowService + '/api/sim/start/',
            submit: backend.workflowService + '/api/sim/submit/'
          },
          tableColumns: [{
            label: '流程名称',
            prop: 'flowName',
            resizable: true
          },
          {
            label: '流程ID',
            prop: 'flowId',
            resizable: true,
            hidden: false
          },
          {
            label: '流程创建者',
            prop: 'flowAdmin',
            resizable: true,
            hidden: false
          },
          {
            label: '流程修改时间',
            prop: 'startTime',
            resizable: true,
            hidden: false
          },
          {
            label: '流程标识',
            prop: 'flowSign',
            resizable: true,
            hidden: false
          },
          {
            label: '系统id',
            prop: 'systemId',
            resizable: true,
            hidden: true
          },
          {
            label: '流程图状态',
            prop: 'flowState',
            resizable: true,
            hidden: true
          },
          {
            label: '流程图内容',
            prop: 'flowContent',
            resizable: true,
            hidden: true
          },
          {
            label: '管理员机构id',
            prop: 'orgId',
            resizable: true,
            hidden: true
          },
          {
            label: '操作',
            width: 200,
            fixed: 'right',
            template: function () {
              return '<template scope="scope">\
              <el-button size="small" type="text" @click="$emit(\'custom-row-op\',scope,\'simu\')">开始仿真</el-button>\
              </template>';
            }
          }
          ],
          baseParams: {
            condition: JSON.stringify({
              flowName: '',
              flowId: '',
              flowState: 'R'
            })
          },
          height: yufp.frame.size().height - 103,
          dialogVisible: false,
          dialogVisibleNext: false,
          viewTitle: '仿真路由参数设置(可选)',
          Title: '下一节点选择',
          agreeRadio: '',
          inputParam: '',
          exampleParam: '{\r"param1": "string1",\r' + '"param2": "string2",\r' + '"param3": "string3"\r' + '}',
          opType: 'start',
          commonParam: {
            flowId: '',
            systemId: 'cmis'
          },
          extParam: {},
          NextNodes: [],
          nextNode: '',
          DoneNodes: [],
          startTime: '',
          currentPage: 1,
          pageSize: 20
        };
      },
      methods: {
        customRowOp: function (scope, op) { // 表格操作按钮事件
          var _this = this;
          // 当前行号：scope.$index
          // 当前行数据：scope.row
          // 当前列对象：scope.column
          // this.$alert('你现在正在操作：' + op + '当前行ID值为：' + scope.row.flowId, '提示');
          if (op == 'simu') {
            _this.DoneNodes = [];
            _this.getdate();
            _this.commonParam.flowId = scope.row.flowId;
            _this.commonParam.systemId = scope.row.systemId;
            _this.opType = 'start';
            _this.dialogVisible = true;
          }
        },
        checkRoute: function (nodes) {
          for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeSign == 'error') {
              this.$message({ duration: 4000, message: '存在路由错误,请选择是否继续仿真', type: 'warning' });
              nodes[i].nodeName = nodes[i].nodeName + '(路由错误,可跳过)';
              if (this.DoneNodes.length > 0) {
                var k = this.DoneNodes.length - 1;
                this.DoneNodes[k].nextNode = nodes[i].nodeId + ';';
                this.DoneNodes[k].nextNodeName = nodes[i].nodeName + ';';
                this.DoneNodes[k].selectNode = nodes[i].nodeId;
                this.DoneNodes[k].nodeSign = nodes[i].nodeSign;
              }
              this.dialogVisibleNext = true;
              return;
            }
          }
          if (nodes.length > 1) {
            this.opType = 'submit';
            this.dialogVisibleNext = true;
          } else {
            this.nextNode = nodes[0].nodeId;
            this.setNodes(nodes);
            this.submitFn();
          }
        },
        setNodes: function (nodes) { // 多个节点
          var nodeIds = '';
          var nodeNames = '';
          var nodeSign = '';
          var nodeType = '';
          var node = {
            nodeId: this.nextNode,
            nodeName: '',
            nodeSign: '',
            nextNode: '',
            nextNodeName: '',
            selectNode: '',
            startTime: this.startTime
          };
          for (var k = 0; k < nodes.length; k++) {
            nodeIds += nodes[k].nodeId + ';';
            nodeNames += nodes[k].nodeName + ';';
            if (this.nextNode == nodes[k].nodeId) {
              nodeSign = nodes[k].nodeSign;
              node.nodeName = nodes[k].nodeName;
              nodeType = nodes[k].nodeType;
            }
          }
          if (this.DoneNodes.length > 0) {
            var i = this.DoneNodes.length - 1;
            this.DoneNodes[i].nextNode = nodeIds;
            this.DoneNodes[i].nextNodeName = nodeNames;
            this.DoneNodes[i].selectNode = this.nextNode;
            this.DoneNodes[i].nodeSign = nodeSign;
          }
          if (nodeType != 'E') {
            this.DoneNodes.push(node);
          } else {
            this.nextNode = '';
          }
        },
        startFn: function () { // 发起流程
          var _this = this;
          _this.getdate();
          if (_this.inputParam != '') {
            _this.extParam = JSON.parse(_this.inputParam);
          }
          var param = _this.commonParam;
          yufp.service.request({
            method: 'POST',
            url: _this.urls.start,
            data: param,
            callback: function (code, message, response) {
              if (response.code == 0) {
                if (response.data != null) {
                  var nodes = response.data;
                  _this.NextNodes = nodes;
                  _this.checkRoute(nodes);
                  _this.dialogVisible = false;
                } else {
                  _this.$message({ duration: 4000, message: '未找到流程的节点信息!', type: 'warning' });
                }
              } else {
                _this.$message({
                  duration: 4000,
                  message: '仿真发起失败:' + response.message,
                  type: 'error'
                });
              }
            }
          });
        },
        submitFn: function () { // 开始仿真
          var _this = this;
          _this.getdate();
          _this.opType = 'loding';
          if (_this.nextNode == null || _this.nextNode == '') {
            return;
          }
          var param = {
            nodeId: _this.nextNode,
            param: _this.extParam
          };
          yufp.service.request({
            method: 'POST',
            url: _this.urls.submit,
            data: param,
            callback: function (code, message, response) {
              if (response.code == 0) {
                if (response.data != null) {
                  var nodes = response.data;
                  _this.NextNodes = nodes;
                  _this.checkRoute(nodes);
                  if (nodes[0].nodeType == 'E') {
                    _this.opType = 'close';
                    _this.$message({ duration: 4000, message: '仿真完成!', type: 'success' });
                  }
                } else {
                  _this.DoneNodes.length = _this.DoneNodes.length - 1;
                }
              } else {
                _this.$message({
                  duration: 4000,
                  message: '仿真失败:' + response.message,
                  type: 'error'
                });
              }
            }
          });
        },
        returnNode: function () { // 选取下一节点方法
          if (this.agreeRadio != '') {
            this.nextNode = this.agreeRadio;
            this.agreeRadio = '';
            this.setNodes(this.NextNodes);
          } else {
            this.$message({ duration: 4000, message: '请选择下一节点!', type: 'warning' });
            return false;
          }
          if (this.nextNode != '') {
            this.submitFn();
          }
          this.dialogVisibleNext = false;
        },
        cancel: function () {
          this.dialogVisibleNext = false;
        },
        Close: function () { // 弹框关闭处理事件
          this.inputParam = [];
          this.dialogVisible = false;
        },
        click: function () { // 路由参数示例格式复用
          var _this = this;
          _this.inputParam = _this.exampleParam;
        },
        getdate: function () { // 获取当前时间并转化格式为yyyy-mm-dd hh:mm:ss
          var d = new Date();
          var year = d.getFullYear();
          var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1;
          var date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
          var hour = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
          var minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
          var seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
          var myWant = year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + seconds;
          this.startTime = myWant;
        },
        SizeChange: function (val) { // 分页-页面大小改变响应事件
          this.pageSize = val;
        },
        CurpageChange: function (val) { // 分页-当前页面改变响应事件
          this.currentPage = val;
        }
      }
    });
  };
});
